package com.youdianstar.app.adapter

import android.app.Activity
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.youdianstar.app.common.ex.setThrottleListener
import com.youdianstar.app.common.util.StrUtil
import com.youdianstar.app.R
import com.youdianstar.app.entity.CardBanner
import com.youdianstar.app.util.CardIcUtil
import com.zhpan.bannerview.BaseBannerAdapter
import com.zhpan.bannerview.BaseViewHolder


/**
 * File: CardBannerAdapter
 * Author: 82149 Create: 2024/1/2 17:14
 * Changes (from 2024/1/2)
 * --------------------------------------------------
 * description: 钱包 card banner
 * ---------------------------------------------------
 *
 */
class CardBannerAdapter (val mContext: Activity, val listener: OnItemListener) : BaseBannerAdapter<CardBanner>(){
    public interface OnItemListener {
        fun onAddCard()

        fun onEditCard(position: Int)
    }

    override fun bindData(
        holder: BaseViewHolder<CardBanner>?, data: CardBanner, position: Int, pageSize: Int) {
        val ivLogo = holder?.findViewById<ImageView>(R.id.icon)
        val tvTitle = holder?.findViewById<TextView>(R.id.tx_item_title)
        val llExp = holder?.findViewById<LinearLayout>(R.id.ll_exp)
        val tvExp = holder?.findViewById<TextView>(R.id.tv_exp)
        val tvDefault = holder?.findViewById<TextView>(R.id.tv_default)
        val tvName = holder?.findViewById<TextView>(R.id.tv_name)
        val btnEdit = holder?.findViewById<TextView>(R.id.btn_edt)

        val ivIconAdd = holder?.findViewById<ImageView>(R.id.iv_card)
        val tvAddTitle = holder?.findViewById<TextView>(R.id.tx_add_title)
        val tvAddHint = holder?.findViewById<TextView>(R.id.tv_add_hint)
        val ivNumberHint = holder?.findViewById<ImageView>(R.id.iv_number_hint)
        val btnAdd = holder?.findViewById<TextView>(R.id.btn_add)

        if (getViewType(position) == 1) {
            ivLogo?.visibility = View.INVISIBLE
            tvTitle?.visibility = View.INVISIBLE
            llExp?.visibility = View.INVISIBLE
            tvName?.visibility = View.INVISIBLE
            btnEdit?.visibility = View.INVISIBLE

            ivIconAdd?.visibility = View.VISIBLE
            tvAddTitle?.visibility = View.VISIBLE
//            tvAddHint?.visibility = View.VISIBLE //25/02/18 隐藏
            ivNumberHint?.visibility = View.VISIBLE
            btnAdd?.visibility = View.VISIBLE
        } else {
            ivLogo?.visibility = View.VISIBLE
            tvTitle?.visibility = View.VISIBLE
            llExp?.visibility = View.VISIBLE
            tvName?.visibility = View.VISIBLE
            btnEdit?.visibility = View.VISIBLE

            ivIconAdd?.visibility = View.INVISIBLE
            tvAddTitle?.visibility = View.INVISIBLE
            tvAddHint?.visibility = View.INVISIBLE
            ivNumberHint?.visibility = View.INVISIBLE
            btnAdd?.visibility = View.INVISIBLE
            data.card?.let {
                //根据卡类型 补充logo,
                if(StrUtil.isEmpty(it.bankName))
                    ivLogo?.setImageResource(CardIcUtil.getStripeDefaultIcon())
                else
                    ivLogo?.setImageResource(CardIcUtil.getStripeBrandIcon(it.bankName))
                tvTitle?.text = it.bankName
                tvName?.text = "****  ****  **** " + it.bankNo
                tvExp?.text = mContext.getString(R.string.wallet_expiryDate)+": "+it.expireTx()
                tvDefault?.visibility = if (it.defaulted) View.VISIBLE else View.GONE
            }
        }

        btnEdit?.setThrottleListener {
            listener.onEditCard(position)
        }

        btnAdd?.setThrottleListener {
            if (getViewType(position) == 1){
                listener.onAddCard()
            }
        }
    }

    override fun getLayoutId(viewType: Int): Int {
        return R.layout.layout_banner_card_wallet
    }

    override fun getViewType(position: Int): Int {
        return mList[position].type
    }
}